package com.xd.common.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.util.UriComponentsBuilder;
import springfox.documentation.PathProvider;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.paths.DefaultPathProvider;
import springfox.documentation.spring.web.paths.Paths;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;
@Configuration
@EnableSwagger2WebMvc
@ConditionalOnProperty(name = "swagger.enable", havingValue = "true")
public class Knife4jConfiguration {
    /**
     * 可指定多个Docket区分不同的分组
     *
     * @return
     */

    @Value("${server.servlet.context-path}")
    private String servletContextPath;
    @Bean("system")
    public Docket defaultApi2() {
        Docket docket=new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder()
                        //.title("swagger-bootstrap-ui-demo RESTful APIs")
                        .description("# 享客达接口文档")
                        .termsOfServiceUrl("***") //http://127.0.0.1:8081/userall(根据配置文件context path)/doc.html
//                        .contact("xx@qq.com")
                        .version("2.0")
                        .build())
                //分组名称
                .groupName("系统登录")
                .select()
                //这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.basePackage("com.xd.core.system.controller"))
                .paths(PathSelectors.any())
                .build();
        return docket;
    }
    @Bean("h5")
    public Docket h5() {
        Docket docket=new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder()
                        //.title("swagger-bootstrap-ui-demo RESTful APIs")
                        .description("# 享客达接口文档")
                        .termsOfServiceUrl("***") //http://127.0.0.1:8081/userall(根据配置文件context path)/doc.html
//                        .contact("xx@qq.com")
                        .version("2.0")
                        .build())
                //分组名称
                .groupName("h5接口")
                .select()
                //这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.basePackage("com.xd.core.h5.controller"))
                .paths(PathSelectors.any())
                .build();
        return docket;
    }

    @Bean("company")
    public Docket company() {
        Docket docket=new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder()
                        //.title("swagger-bootstrap-ui-demo RESTful APIs")
                        .description("# 享客达接口文档")
                        .termsOfServiceUrl("***") //http://127.0.0.1:8081/userall(根据配置文件context path)/doc.html
//                        .contact("xx@qq.com")
                        .version("2.0")
                        .build())
                //分组名称
                .groupName("资方-代理管理")
                .select()
                //这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.basePackage("com.xd.core.company.controller"))
                .paths(PathSelectors.any())
                .build();
        return docket;
    }
    @Bean("business")
    public Docket business() {
        Docket docket=new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder()
                        //.title("swagger-bootstrap-ui-demo RESTful APIs")
                        .description("# 享客达接口文档")
                        .termsOfServiceUrl("***") //http://127.0.0.1:8081/userall(根据配置文件context path)/doc.html
//                        .contact("xx@qq.com")
                        .version("2.0")
                        .build())
                //分组名称
                .groupName("业务管理")
                .select()
                //这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.basePackage("com.xd.core.business.controller"))
                .paths(PathSelectors.any())
                .build();
        return docket;
    }

    @Bean("product")
    public Docket product() {
        Docket docket=new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder()
                        //.title("swagger-bootstrap-ui-demo RESTful APIs")
                        .description("# 享客达接口文档")
                        .termsOfServiceUrl("***") //http://127.0.0.1:8081/userall(根据配置文件context path)/doc.html
//                        .contact("xx@qq.com")
                        .version("2.0")
                        .build())
                //分组名称
                .groupName("产品接口")
                .select()
                //这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.basePackage("com.xd.core.product.controller"))
                .paths(PathSelectors.any())
                .build();
        return docket;
    }

    @Bean("debt")
    public Docket debt() {
        Docket docket=new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder()
                        //.title("swagger-bootstrap-ui-demo RESTful APIs")
                        .description("# 享客达接口文档")
                        .termsOfServiceUrl("***") //http://127.0.0.1:8081/userall(根据配置文件context path)/doc.html
//                        .contact("xx@qq.com")
                        .version("2.0")
                        .build())
                //分组名称
                .groupName("债权管理")
                .select()
                //这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.basePackage("com.xd.core.debt.controller"))
                .paths(PathSelectors.any())
                .build();
        return docket;
    }

    @Bean("bill")
    public Docket bill() {
        Docket docket=new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder()
                        //.title("swagger-bootstrap-ui-demo RESTful APIs")
                        .description("# 享客达接口文档")
                        .termsOfServiceUrl("***") //http://127.0.0.1:8081/userall(根据配置文件context path)/doc.html
//                        .contact("xx@qq.com")
                        .version("2.0")
                        .build())
                //分组名称
                .groupName("账单管理")
                .select()
                //这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.basePackage("com.xd.core.bill.controller"))
                .paths(PathSelectors.any())
                .build();
        return docket;
    }

    @Bean("finance")
    public Docket finance() {
        Docket docket=new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder()
                        //.title("swagger-bootstrap-ui-demo RESTful APIs")
                        .description("# 享客达接口文档")
                        .termsOfServiceUrl("***") //http://127.0.0.1:8081/userall(根据配置文件context path)/doc.html
//                        .contact("xx@qq.com")
                        .version("2.0")
                        .build())
                //分组名称
                .groupName("财务管理")
                .select()
                //这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.basePackage("com.xd.core.finance.controller"))
                .paths(PathSelectors.any())
                .build();
        return docket;
    }



    @Bean("api")
    public Docket api() {
        Docket docket=new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder()
                        //.title("swagger-bootstrap-ui-demo RESTful APIs")
                        .description("# 享客达接口文档")
                        .termsOfServiceUrl("***") //http://127.0.0.1:8081/userall(根据配置文件context path)/doc.html
//                        .contact("xx@qq.com")
                        .version("2.0")
                        .build())
                //分组名称
                .groupName("三方接口")
                .select()
                //这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.basePackage("com.xd.core.api.controller"))
                .paths(PathSelectors.any())
                .build();
        return docket;
    }

    @Bean("mission")
    public Docket mission() {
        Docket docket=new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder()
                        //.title("swagger-bootstrap-ui-demo RESTful APIs")
                        .description("# 享客达接口文档")
                        .termsOfServiceUrl("***") //http://127.0.0.1:8081/userall(根据配置文件context path)/doc.html
//                        .contact("xx@qq.com")
                        .version("2.0")
                        .build())
                //分组名称
                .groupName("任务中心")
                .select()
                //这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.basePackage("com.xd.core.mission.controller"))
                .paths(PathSelectors.any())
                .build();
        return docket;
    }
    /**
     * 重写 PathProvider ,解决 context-path 重复问题
     * @return
     */
    @Bean
    public PathProvider pathProvider() {
        return new DefaultPathProvider() {
            @Override
            public String getOperationPath(String operationPath) {
                operationPath = operationPath.replaceFirst(servletContextPath, "/");
                UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromPath("/");
                return Paths.removeAdjacentForwardSlashes(uriComponentsBuilder.path(operationPath).build().toString());
            }

            @Override
            public String getResourceListingPath(String groupName, String apiDeclaration) {
                apiDeclaration = super.getResourceListingPath(groupName, apiDeclaration);
                return apiDeclaration;
            }
        };
    }
}
